package com.zuimei.zuimei.fragments;


import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v7.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.viewpagerindicator.TabPageIndicator;
import com.zuimei.zuimei.R;
import com.zuimei.zuimei.adapters.FindAppFragmentPagerAdapter;
import com.zuimei.zuimei.base.BaseFragment;
import com.zuimei.zuimei.utils.SlidingMenuUtil;
import com.zuimei.zuimei.widgets.MyTitleBar;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 发现应用
 */

public class FindAppFragment extends BaseFragment {
    @Bind(R.id.tpi_indicator)
    TabPageIndicator tpi_indicator;
    @Bind(R.id.mtb_title)
    MyTitleBar mtb_title;
    @Bind(R.id.vp_content)
    ViewPager vp_content;
    /**
     * tab标题
     */
    private static final String[] TAB_TITLE = new String[]{
            "精选", "最新", "周边", "合辑", "美友"
    };
    private SlidingMenu mMenu;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_find_app;
    }

    @Override
    protected LayoutInflater setViewStyle(LayoutInflater inflater) {
        Context context = new ContextThemeWrapper(mActivity, R.style.MyIndicator);
        return inflater.cloneInContext(context);
    }

    @Override
    public void init() {
        mtb_title.setSearchBtnVisiable(View.VISIBLE);
        //ViewPager的adapter
        FindAppFragmentPagerAdapter adapter = new FindAppFragmentPagerAdapter
                (getChildFragmentManager(), TAB_TITLE);
        vp_content.setAdapter(adapter);
        SlidingMenuUtil util = SlidingMenuUtil.getInstance();
        mMenu = util.getSlideMenu(mActivity);
        //vp_content.setCurrentItem(0);
        //实例化TabPageIndicator，并与ViewPager关联
        tpi_indicator.setViewPager(vp_content);
        //如果要对ViewPager设置监听，用indicator设置就行了]
        final float density = getResources().getDisplayMetrics().density;
        tpi_indicator.setBackgroundColor(0xFFCCCCCC);
        tpi_indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int
                    positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    mMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                } else {
                    mMenu.setTouchModeAbove(SlidingMenu.LEFT);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick({R.id.mtb_title})
    public void onClick(View view) {
        mMenu.toggle();
    }
}
